package com.skyline.wo.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.service.CommentService;
import com.skyline.common.bean.Page;
import com.skyline.common.util.FreeMarkerUtils;
import com.skyline.feed.service.PersonalFeedService;
import com.skyline.feed.type.FeedType;
import com.skyline.wo.dao.ShortMessageDao;
import com.skyline.wo.model.ShortMessage;
import com.skyline.wo.service.ShortMessageService;

@Service("shortMessageService")
public class ShortMessageServiceImpl implements ShortMessageService {

	@Autowired
	private ShortMessageDao shortMessageDao;

	@Autowired
	private CommentService commentService;

	@Autowired
	private PersonalFeedService personalFeedService;

	@SuppressWarnings("deprecation")
	@Override
	public String addShortMessage(Long ownerId, String ownerNickname, String ownerPortrait, String text) {
		String newMessageId = shortMessageDao.insertShortMessage(ownerId, ownerNickname, ownerPortrait, text, null, null, null, null, null,
				null, null, null, null, null, null);
		if (newMessageId != null) {
			personalFeedService.addFeed(newMessageId, "短消息", text, ownerId, ownerNickname, ownerPortrait, ownerId, FeedType.SHORTMESSAGE);
		}
		return newMessageId;
	}

	@SuppressWarnings("deprecation")
	@Override
	public String forwardShortMessage(String id, Long ownerId, String ownerNickname, String ownerPortrait, String comment) {

		ShortMessage fm = shortMessageDao.queryShortMessage(id);
		String messageId = null;
		String feedContent=null;
		// 这说明，所转来的短消息，那个用户也是从别人那里转来的，所以不用修改源用户的信息
		if (fm.getComment() != null) {
			List<String> comments = fm.getComments();
			List<Long> commentterIds = fm.getCommetterIds();
			List<String> commentterNicknames = fm.getCommetterNicknames();
			if (comments == null || commentterIds == null || commentterNicknames == null) {
				comments = new ArrayList<String>();
				commentterIds = new ArrayList<Long>();
				commentterNicknames = new ArrayList<String>();
			}
			comments.add(fm.getComment());
			commentterIds.add(fm.getOwnerId());
			commentterNicknames.add(fm.getOwnerNickname());
			messageId = shortMessageDao.insertShortMessage(ownerId, ownerNickname, ownerPortrait, fm.getText(), fm.getSourceOwnerId(),
					fm.getSourceOwnerNickname(), comment, commentterIds, commentterNicknames, comments, null, null, null, null, null);
			feedContent=FreeMarkerUtils.getShortMessage(fm.getText(),  fm.getSourceOwnerId(), fm.getSourceOwnerNickname(), comment, commentterIds, commentterNicknames, comments);
			
		} else {
			messageId = shortMessageDao.insertShortMessage(ownerId, ownerNickname, ownerPortrait, fm.getText(), fm.getOwnerId(),
					fm.getOwnerNickname(), comment, null, null, null, null, null, null, null, null);
			feedContent=FreeMarkerUtils.getShortMessage(fm.getText(),  fm.getOwnerId(), fm.getOwnerNickname(), comment, null, null, null);
		}
		personalFeedService.addFeed(messageId, "短消息", feedContent, ownerId, ownerNickname, ownerPortrait, ownerId, FeedType.SHORTMESSAGE);
		return messageId;
	}

	@Override
	public ShortMessage getShortMessageById(String id) {
		return shortMessageDao.queryShortMessage(id);
	}

	@Override
	public List<ShortMessage> getShortMessageByOwnerId(Long ownerId, Page page) {
		return shortMessageDao.queryShortMessageByOwnerId(ownerId, page);
	}

	@Override
	public void deleteShortMessage(String id, Long actionerId) {
		//shortMessageDao.deleteShortMessage(id, actionerId);
	}

}
